package com.happy.cat.project.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.cat.project.system.domain.UserBalancePetValueDetail;
import com.happy.cat.project.system.mapper.UserBalancePetValueDetailMapper;
import com.happy.cat.project.system.service.IUserBalancePetValueDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 账号余额明细(猫粮)Service业务层处理
 *
 * @author verne
 * @date 2020-08-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserBalancePetValueDetailServiceImpl extends ServiceImpl<UserBalancePetValueDetailMapper, UserBalancePetValueDetail> implements IUserBalancePetValueDetailService {

    @Override
    public Double amountByAccountId(Long id) {
        List<UserBalancePetValueDetail> list =
                this.lambdaQuery().eq(UserBalancePetValueDetail::getAccountId, id)
                        .eq(UserBalancePetValueDetail::getAmountType, 1)
                        .eq(UserBalancePetValueDetail::getSymbol, 0).list();
        if(list.size()>0){
            return list.stream().mapToDouble(UserBalancePetValueDetail::getAmount).sum();
        }
        return 0.00;
    }

    @Override
    public Double rechargeByAccountId(Long id) {
        List<UserBalancePetValueDetail> list =
                this.lambdaQuery().eq(UserBalancePetValueDetail::getAccountId, id)
                        .eq(UserBalancePetValueDetail::getAmountType, 1)
                        .eq(UserBalancePetValueDetail::getType,1)
                        .eq(UserBalancePetValueDetail::getSymbol, 1).list();
        return list.stream().mapToDouble(UserBalancePetValueDetail::getAmount).sum();
    }

    @Override
    public Date firstRechargeTime(Long id) {
        UserBalancePetValueDetail detail = this.lambdaQuery().eq(UserBalancePetValueDetail::getAccountId, id)
                .eq(UserBalancePetValueDetail::getAmountType, 1)
                .eq(UserBalancePetValueDetail::getType, 1)
                .eq(UserBalancePetValueDetail::getSymbol, 1)
                .orderByAsc(UserBalancePetValueDetail::getChangeTime)
                .one();
        if(detail!= null ){
            return detail.getChangeTime();
        }
        return null;
    }


}
